package com.cg.client;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cg.util.DataUtil;
import com.cg.util.HTTPUtils;
import com.cg.util.PropertiesManager;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

public class CGClient {
	
	private static Log log = LogFactory.getLog(CGClient.class.getName());
	private static final String userSearch = PropertiesManager.getGenericConfigProperties().getProperty("uri.searchService");
	
	public UsersInRadius getUsersInRadius(double startAddressGPSlat,
			double startAddressGPSlng,int startRadius,double endAddressGPSlat,
			double endAddressGPSlng, int endRadius ,  Date startTime, Date endTime) {

		String result = null;
	    UsersInRadius journeyList = null;

		try {

			StringBuffer requestParameters = new StringBuffer();
			if (startAddressGPSlat != 0 && startAddressGPSlng != 0
					&& endAddressGPSlat != 0 && endAddressGPSlng != 0
					&& startRadius > 0 && endRadius > 0 && startTime != null && endTime != null) {
				
				
				requestParameters.append("format=json");
				requestParameters.append("&");
				requestParameters.append("startLat=" + startAddressGPSlat);
				requestParameters.append("&");
				requestParameters.append("startLng=" + startAddressGPSlng);
				requestParameters.append("&");
				requestParameters.append("startRadius=" + startRadius);
				requestParameters.append("&");
				requestParameters.append("endLat=" + endAddressGPSlat);
				requestParameters.append("&");
				requestParameters.append("endLng=" + endAddressGPSlng);
				requestParameters.append("&");
				requestParameters.append("endRadius=" + endRadius);
				requestParameters.append("&");
				requestParameters.append("from="
						+ URLEncoder.encode(DataUtil.convertDate2String(
							startTime, "yyyy-MM-dd'T'HH:mm:ssZ"), "UTF-8"));
				//requestParameters.append("from="+"2011-09-12T05%3A00%3A00%2B0000");
				requestParameters.append("&");
				//requestParameters.append("to="+"2011-09-12T10%3A00%3A11%2B0000");
				requestParameters.append("to="
						+ URLEncoder.encode(DataUtil.convertDate2String(
								endTime, "yyyy-MM-dd'T'HH:mm:ssZ"), "UTF-8"));

				System.out.println("Printing request"+userSearch+requestParameters.toString());
				        result = HTTPUtils.sendGetRequest(userSearch,requestParameters.toString());

				
				log.info("getUsersInRadius response: " + result + "\n");
				        
				        
				        
				Gson gson = new Gson();
				// deserializes json into UsersInRadius

			journeyList = gson.fromJson(result, UsersInRadius.class);
			
			}
		} catch (JsonSyntaxException e) {
			log.error("Error in parsing CG response");
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			e.printStackTrace(new PrintStream(out, true));
			log.error(out.toString().replaceAll("\t", " "));
		} catch (UnsupportedEncodingException e) {
			log.error("Error encoding the request");
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			e.printStackTrace(new PrintStream(out, true));
			log.error(out.toString().replaceAll("\t", " "));
		} catch (Exception e) {
			log.error("Error callng CG API");
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			e.printStackTrace(new PrintStream(out, true));
			log.error(out.toString().replaceAll("\t", " "));
		}

		return journeyList;
	}

}
